package com.kun.portal.webframe.service;

import com.kun.portal.webframe.dao.GoodsInventoryDao;
import com.kun.portal.webframe.dao.StockInDao;
import com.kun.portal.webframe.dao.SupplierMgrDao;
import com.kun.portal.webframe.po.GoodsInventoryPo;
import com.kun.portal.webframe.po.StockInPo;
import com.kun.portal.webframe.po.SupplierMgrPo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/7/2.
 */
@Service
public class InventoryManagementService {
    @Autowired
    private GoodsInventoryDao goodsInventoryDao;
    @Autowired
    private StockInDao stockInDao;
    @Autowired
    private SupplierMgrDao supplierMgrDao;
    public List<GoodsInventoryPo> findAllGoodsInventoryPo(){
      return goodsInventoryDao.findAll();
    }
    public void saveGoodsInventoryPo(GoodsInventoryPo goodsInventoryPo){
        goodsInventoryDao.save(goodsInventoryPo);
    }
    public void saveStockInPo(StockInPo stockInPo){
        stockInDao.save(stockInPo);
    }
    public List<StockInPo> findAllStockInRecord(){
        return stockInDao.findAll();
    }
    public  List<StockInPo> checkStockInRecordPo(String goodsCategory, String goodsCode, Date stockDate,String supplier){

        Specification query = new Specification<StockInPo>() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root<StockInPo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<javax.persistence.criteria.Predicate> pre= new ArrayList<>();
                if(StringUtils.isNotBlank(goodsCategory)){
                    pre.add(cb.like(root.get("goodsInventoryPo").get("goodsCategory"), "%"+goodsCategory+"%"));
                }
                if(StringUtils.isNotBlank(goodsCode )){
                    pre.add(cb.like(root.get("goodsInventoryPo").get("goodsCode"), "%"+goodsCode+"%"));
                }
                if(null != stockDate){
                    pre.add(cb.equal(root.get("stockDate"), stockDate));
                }
                if(StringUtils.isNotBlank(supplier )){
                    pre.add(cb.like(root.get("supplier"), "%"+supplier+"%"));
                }
                Predicate predicate = cb.and(pre.toArray(new Predicate[pre.size()]));
                return predicate;
            }
        };
        return stockInDao.findAll(query);

    }
    public  List<SupplierMgrPo> findAllSupplierMgrPo(){
        return supplierMgrDao.findAll();
    }
    public SupplierMgrPo saveSupplierPo(SupplierMgrPo supplierMgrPo){
        return  supplierMgrDao.save(supplierMgrPo);
    }
    public  List<SupplierMgrPo> checkSupplierMgrPo(String companyName ,String supplierAbbr ){

        Specification query = new Specification<SupplierMgrPo>() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root<SupplierMgrPo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<javax.persistence.criteria.Predicate> pre= new ArrayList<>();
                if(StringUtils.isNotBlank(companyName)){
                    pre.add(cb.like(root.get("companyName"), "%"+companyName+"%"));
                }
                if(StringUtils.isNotBlank(supplierAbbr)){
                    pre.add(cb.like(root.get("supplierAbbr"), "%"+supplierAbbr+"%"));
                }
                Predicate predicate = cb.and(pre.toArray(new Predicate[pre.size()]));
                return predicate;
            }
        };
        return supplierMgrDao.findAll(query);

    }
    public void deleteSupplierMgrPo(SupplierMgrPo supplierMgrPo){
        supplierMgrDao.delete(supplierMgrPo);
    }





}
